﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;

/// <summary>
/// Summary description for WebServiceOS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebServiceOS : System.Web.Services.WebService {

    public WebServiceOS () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public void CallAfterEvery5Min()
    {
        //Sendmail.sendEmail("anamuliut@gmail.com","onlineSMS","body");
        try {
            string sql = @"
                SELECT Top 1 [SMSID]
                      ,[SMS]
                      ,[LoginID]
                        ,[Masking]
                      ,[SendToGroupIDs]
                  FROM [OnlineSMS].[dbo].[SMS_Job]
                  where [StatusID]=0 and [JobSubmissionDate]<= GETDATE()
                ";

            DataSet ds = DatabaseManager.ExecSQL(sql);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                bool isStated = false;
                
                sql = @"
                SELECT [ContactID]
                      ,[MobileNo]
                      ,[GroupID]
                  FROM [OnlineSMS].[dbo].[Contact] where GroupID in (" + dr["SendToGroupIDs"].ToString() + @")
                    order by [ContactID] 
                ";
                ds = DatabaseManager.ExecSQL(sql);
                foreach (DataRow nos in ds.Tables[0].Rows)
                {
                    string no = nos["MobileNo"].ToString();
                    if (no.Trim() != "")
                    {
                        if (SendSMS.sendSMS(dr["LoginID"].ToString(), dr["Masking"].ToString(), no, dr["SMS"].ToString()))
                        {
                            sql = @"
                                    update [OnlineSMS].[dbo].[SMS_Job] set SendToCount+=1, LastUpdateDate=GETDATE() where [SMSID]=" + dr["SMSID"].ToString() + @"
                                    ";

                            if (!isStated)
                            {
                                sql += "update [OnlineSMS].[dbo].[SMS_Job] set StatusID=1, JobStartedDate=GETDATE() where [SMSID]=" + dr["SMSID"].ToString();
                                isStated = true;
                            }
                            DatabaseManager.ExecSQL(sql);


                        }
                        else
                        {
                            sql = @"
                                    update [OnlineSMS].[dbo].[SMS_Job] set NotSendToCount+=1,NotSendTo+=',"+no+"', LastUpdateDate=GETDATE() where [SMSID]=" + dr["SMSID"].ToString() + @"
                                    ";
                            if (!isStated)
                            {
                                sql += "update [OnlineSMS].[dbo].[SMS_Job] set StatusID=1, JobStartedDate=GETDATE() where [SMSID]=" + dr["SMSID"].ToString();
                                isStated = true;
                            } 
                            DatabaseManager.ExecSQL(sql);
                        }
                    }
                }

            }

        }
        catch (Exception ex)
        {
           // Sendmail.sendEmail("anamuliut@gmail.com", "IEB Server Error for OnlineSMS", ex.Message);
        }
    }
}
